import { _decorator, Camera, Component, Node, tween, v3, Vec3 } from 'cc';
import { Global } from '../Global';
import { AudioManger } from '../AudioManger';
import { Clips } from '../Enums';
import { InstructAction } from './house/InstructAction';
const { ccclass, property } = _decorator;

@ccclass('HomeView')
export class HomeView extends Component {
    @property(Node)
    private gameNode: Node = null;
    @property(InstructAction)
    private instruct: InstructAction = null;
    @property(Camera)
    private scene_camera: Camera = null;

    @property(Node)
    top_area: Node = null;
    @property(Node)
    left_area: Node = null;
    @property(Node)
    right_area: Node = null;
    
    private top_src_pos:Vec3 = null;
    private left_src_pos:Vec3 = null;
    private right_src_pos:Vec3 = null;


    start() {
        Global.home_view = this;
        Global.scene_camera = this.scene_camera;

        this.top_src_pos = this.top_area.position.clone();
        this.left_src_pos = this.left_area.position.clone();
        this.right_src_pos = this.right_area.position.clone();
    }

    onGamePlay(){
        this.gameNode.active = true;
        this.node.active = false;
    }

    onHouseInstruct(){
        AudioManger.instance.playSound(Clips.btn_1);
        this.hideInfo();
        this.scheduleOnce(() => {
            this.instruct.open();
        }, 0.2);
        // this.instruct.open();
    }

    public backToHome(){
        this.gameNode.active = false;
        this.node.active = true;
    }

    public showInfo(){
        tween(this.top_area)
            .to(0.2, {position: v3(this.top_src_pos.x, this.top_src_pos.y, this.top_src_pos.z)})
            .start();
        tween(this.left_area)
            .to(0.2, {position: v3(this.left_src_pos.x, this.left_src_pos.y, this.left_src_pos.z)})
            .start();
        tween(this.right_area)
            .to(0.2, {position: v3(this.right_src_pos.x, this.right_src_pos.y, this.right_src_pos.z)})
            .start();
    }

    public hideInfo(){
        tween(this.top_area)
            .to(0.2, {position: v3(this.top_src_pos.x, this.top_src_pos.y+200, this.top_src_pos.z)})
            .start();
        tween(this.left_area)
            .to(0.2, {position: v3(this.left_src_pos.x-200, this.left_src_pos.y, this.left_src_pos.z)})
            .start();
        tween(this.right_area)
            .to(0.2, {position: v3(this.right_src_pos.x+200, this.right_src_pos.y, this.right_src_pos.z)})
            .start();
    }

    update(deltaTime: number) {
        
    }
}


